package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.sql.ClientInfoStatus;
import java.util.List;

/**
 * 员工管理模块
 *
 * @Name EmployeeController
 * @Author stella
 * @Date 2023-03-01 11:12 AM
 */
//员工管理模块
@RestController
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    //登陆
    @PostMapping("/employee/login")
    public ResultInfo login(@RequestBody Employee employee){
        //调用service登陆
        ResultInfo resultInfo = employeeService.login(employee.getUsername(),employee.getPassword());
        //返回结果
        return resultInfo;
    }

    @Autowired
    private HttpSession session;

    //员工退出
    @PostMapping("/employee/logout")
    public ResultInfo logout(){
        // 1.自毁session
        session.invalidate();
        //返回成功
        return ResultInfo.success();
    }

    //员工列表
    @GetMapping("/employee/find")
    public ResultInfo find(String name){//接受请求的参数
        //调用service查询
        List<Employee> list = employeeService.findList(name);
        //返回结果
        return ResultInfo.success(list);
    }

    // 新增员工
    @PostMapping("/employee")
    public ResultInfo insert(@RequestBody Employee e){
        employeeService.addNew(e);
        return ResultInfo.success();
    }

    // 回显员工
    @GetMapping("/employee/{id}")
    public ResultInfo findOne(@PathVariable Long id){
        Employee employee = employeeService.findOne(id);
        return ResultInfo.success(employee);
    }

    // 修改员工
    @PutMapping("/employee")
    public ResultInfo update(@RequestBody Employee e){
        employeeService.update(e);
        return ResultInfo.success();
    }


}
